/**
 * 
 */
package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteNewsSearchDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;

/**
 * @author fengbin  createTime:2009-7-20
 *
 */
public class HomeSiteNewsSearchDaoImpl extends HibernateDaoSupport implements IHomeSiteNewsSearchDao {

	/**********
	 * 通过类目id,获取该类目下所有的新闻
	 */
	public PageResultSet loadHomeSiteNewsListByClassId(long classId,String keyWords,int page, int pageSize) throws DataAccessException {
		
		
		 //初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);
		
		if(page == 0){
			page = HomeSiteNewsConstant.DEFAULT_PAGE;
		}
		
		if(pageSize == 0){
			pageSize = HomeSiteNewsConstant.DEFAULT_PAGE_SIZE;
		}

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from HomeSiteNews as hn where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
		 // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        
        if(classId!=0){
        	stringBuffer.append(" and hn.homeSiteNewsClass.id=? ");
            list.add(classId);
        }
        
        
        if(keyWords!=null  && !keyWords.equals("")){
        	stringBuffer.append(" and hn.title  like ? ");
            list.add("%"+keyWords+"%");
        }
        
        
        stringBuffer.append(" and hn.isDel =0  and hn.audite = 1");
        stringBuffer.append(" order by hn.createDatetime desc ");
        
		
        String[] strParams = Tool.toArray(list);
        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
                session, strParams);
	
	   return pageResultSet;
		
		
	}

}
